package com.ulook.lookplanet.ui.home.login;

import android.app.Application;

import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;
import androidx.annotation.NonNull;

import com.ulook.lookplanet.data.source.LoginDataSource;
import com.ulook.lookplanet.data.repository.LoginRepository;

/**
 * ViewModel provider factory to instantiate LoginViewModel.
 * ViewModel提供程序工厂来实例化LoginViewModel。
 * Required given LoginViewModel has a non-empty constructor
 * 给定的LoginViewModel必须有一个非空构造函数
 */
public class LoginViewModelFactory implements ViewModelProvider.Factory {

    private Application application;

    public LoginViewModelFactory(Application application){
        this.application = application;
    }

    @NonNull
    @Override
    @SuppressWarnings("unchecked")
    public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
        if (modelClass.isAssignableFrom(LoginViewModel.class)) {
            LoginDataSource dataSource = new LoginDataSource();
            LoginRepository repository = LoginRepository.getInstance(dataSource);
            LoginViewModel viewModel = new LoginViewModel(repository, application);
            return (T) viewModel;
        } else {
            throw new IllegalArgumentException("Unknown ViewModel class");
        }
    }
}